const CloudDBZoneWrapper = require('./CloudDBZoneWrapper')


let myHandler = async function (event, context, callback, logger) {
  logger.info(event);

  // do something here

  const cloudDBZoneWrapper = new CloudDBZoneWrapper(logger)
  //
  // const data = await cloudDBZoneWrapper.queryAll()
  /**
   * 本地测试不需要序列化
   * 远程调用需要序列化
   */
  const {payload,action} = JSON.parse(event.body)
  if (action === 'add') {
    // 添加用户
    await cloudDBZoneWrapper.add(payload)
    return callback({
      code: 0,
      status: true
    })
  }
  if (action === 'query') {
    const queryResult = await cloudDBZoneWrapper.query(payload)
    const data = queryResult.map(item => Object.fromEntries(item.getFieldMap()))
    return callback({
      code: 0,
      status: true,
      data
    })

  }
};

export { myHandler };